﻿using System.Windows;
using System.Windows.Input;

namespace FlagTunes.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the addMusicMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void addMusicMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.ShowDialog();

            var updateWindow = new LibraryUpdateWindow(this.mainWindowViewModel);
            updateWindow.Show();
            this.mainWindowViewModel.AddSongs(dialog.SelectedPath);
        }

        /// <summary>
        /// Handles the Click event of the stopButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            this.mainWindowViewModel.StopLoadedSong();
        }

        /// <summary>
        /// Handles the MouseDoubleClick event of the song control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void song_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            this.mainWindowViewModel.StopLoadedSong();
            this.mainWindowViewModel.PlayCommand.Execute(null);
        }

        /// <summary>
        /// Handles the Closing event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.mainWindowViewModel.CloseTunesManager();
        }

        /// <summary>
        /// Handles the KeyUp event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.OriginalSource != this.searchTextBox)
            {
                switch (e.Key)
                {
                    case Key.Enter:
                        this.mainWindowViewModel.StopLoadedSong();
                        this.mainWindowViewModel.PlayCommand.Execute(null);
                        break;

                    case Key.Space:
                        this.mainWindowViewModel.PlayOrContinueLoadedSong();
                        break;
                }
            }
        }

        /// <summary>
        /// Handles the GotFocus event of the searchTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void searchTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.mainWindowViewModel.SearchPattern = string.Empty;
        }
    }
}